package com.cycling.app.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.cycling.app.R;
import com.cycling.app.bean.User;
import com.cycling.app.utils.DribblePrefs;

/**
 * Created by ZhouLei on 2017/5/6.
 */

public class RegisterActivity extends BaseActivity {

    private EditText edit_username;
    private EditText edit_mobile;
    private EditText edit_pass;
    private EditText edit_pass_confirm;

    @Override
    public int layoutResId() {
        return R.layout.activity_register;
    }

    @Override
    public void onActivityCreate(@Nullable Bundle savedInstanceState) {
        setTitle("注册账号");
        initView();
    }
    private void initView() {
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_mobile = (EditText) findViewById(R.id.edit_mobile);
        edit_pass = (EditText) findViewById(R.id.edit_password);
        edit_pass_confirm = (EditText) findViewById(R.id.edit_password_confirm);
    }
    private String getString(EditText edit) {
        if (edit == null) return "";
        return edit.getText().toString();
    }
    public void registerNow(View view) {
        String username = getString(edit_username);
        String mobile = getString(edit_mobile);
        String pass = getString(edit_pass);
        String pass_confirm = getString(edit_pass_confirm);
        String msg = TextUtils.isEmpty(username) ? "请输入用户名"
                : TextUtils.isEmpty(mobile) ? "请输入手机号"
                : TextUtils.isEmpty(pass) ? "请输入密码"
                : TextUtils.isEmpty(pass_confirm) ? "请确认密码"
                : !TextUtils.equals(pass, pass_confirm) ? "两次输入的密码不一致" : "";
        if (!TextUtils.isEmpty(msg)) {
            Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
            return;
        }

        DribblePrefs.get().login(new User(1, username, mobile, 100.00));
        Toast.makeText(RegisterActivity.this, "注册成功，已自动登录", Toast.LENGTH_SHORT).show();
        setResult(RESULT_OK);
        finish();
    }
}
